// 首页
homeApp.controller("homeIndexCtr",function($scope,$location,userService,commonService){
	$(".start_loading").addClass("loading");

	if(userService.userIsLogin()){
		$scope.userIsLogin = true;
	}else{
		$scope.userIsLogin = false;
	}

	var curPage = 1;
	var count = 0;

	var formDataStr = $.param({
		p: curPage,
		pageSize: 12
	})

	commonService.httpReq(indexUrl,formDataStr,function(response){
		if(response.STATUS == SUCCESS){
			var res = response.RESULT;
			$.each(res.list,function(index,value){
				value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
				value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
				value.description = (value.description=="")?"懒得留描述":value.description;
				value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
				value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
			})
			$scope.res = res;
			count = res.count;
			if(count > 12){
				$scope.showMe = true;
			}else {
				$scope.showMe = false;
			}
			$(".start_loading").removeClass("loading");
		}
	})

	$scope.search = function($event){
		var keyStr = $scope.key;

		setTimeout(function(){
			if($scope.key == keyStr){
				curPage = 1;

				var formDataStr = $.param({
					p: curPage,
					pageSize: 12,
					key: $scope.key
				})

				commonService.httpReq(teacherSearch,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var res = response.RESULT;
						$.each(res.list,function(index,value){
							value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
							value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
							value.description = (value.description=="")?"懒得留描述":value.description;
							value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
							value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
						})
						$scope.res = res;
						count = res.count;
						if(count > 10){
							$scope.showMe = true;
						}else {
							$scope.showMe = false;
						}
					}
				})

			}
		},500)

	}

	$scope.page = function($event){
		if(curPage * 10 < count) {
			$event.target.innerHTML = "玩命加载中...请稍候...";	
			++curPage;
		}else {
			$event.target.innerHTML = "到底啦，到底啦，到底啦...";
			return false;
		}

		var formDataStr = $.param({
			p: curPage,
			pageSize: 12,
			key: $scope.key
		})

		commonService.httpReq(teacherSearch,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				var html = "";
				$.each(res.list,function(index,value){
					value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
					value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
					value.description = (value.description=="")?"懒得留描述":value.description;
					value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
					value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;

					html+='<li>';
					html+='<a href="#/home/detail/'+value.id+'">';
					html+='		<div class="start_headimg">';
					html+='			<img src="'+value.pic+'">';
					html+='			<div class="start_comment_num">'+value.comment_num+'</div>';
					html+='		</div>';
					html+='		<div class="start_name">';
					html+=			value.name;
					html+='		</div>';
					html+='		<div class="start_description">';
					html+=			value.school_name+'<br>'+value.academy+' '+value.description;
					html+='		</div>';
					html+='		<div class="start_good_and_bad">';
					html+='			<div class="start_num start_good_num">';
					html+='				<button>'+value.good+'</button>';
					html+='			</div>';
					html+='			<div class="start_num start_bad_num">';
					html+='				<button>'+value.bad+'</button>';
					html+='			</div>';
					html+='		</div>';
					html+='	</a>';
					html+='</li>';
				})

				$(".start_list > ul").append(html);
				if(curPage * 10 < res.count) {
					$event.target.innerHTML = "点击加载更多...";	
				}else {
					$event.target.innerHTML = "到底啦，到底啦，到底啦...";
				}
				
			}
		})
	}
})

// 详情
homeApp.controller("detailCtr",function($scope,$routeParams,$location,$sce,commonService ,userService){
	$(".start_loading").addClass("loading");
	//return false;
	
	$scope.canPost = false;
	var uuid = '';

	if(!userService.userIsLogin()){
		$scope.userIsLogin = false;
	}else{
		$scope.userIsLogin = true;
		uuid = userService.getLocalStorage("start-uuid");
	}

	var id = $routeParams.id;
	var curPage = 1;
	var formDataStr = $.param({
		tid: id,
		uuid: uuid,
	})


	var formDataStr = $.param({
		tid: id,
		uuid: uuid,
	})

	commonService.httpReq(detailUrl,formDataStr,function(response){
		if(response.STATUS == SUCCESS){
			var res = response.RESULT;
			res.teacher.pic = (res.teacher.pic == "")? default_headimg_url : urlPic + res.teacher.pic;
			$scope.res = res;
			$scope.goodDisable = res.good;
			$scope.badDisable = res.bad;
			if(localStorage.getItem("start-name") != null){
				$scope.username = userService.getLocalStorage("start-name");
				$scope.userpic = urlPic + userService.getLocalStorage("start-pic");
			}else{
				$scope.username = "新用户";
				$scope.userpic = default_headimg_url;
			}

			var formDataStr = $.param({
				tid:id,
				uuid: uuid,
			})

			commonService.httpReq(tagsUrl,formDataStr,function(response){
				var tags = response.RESULT.tags;
				if(tags.length != 0) {
					$.each(tags,function(index,value){
						value.is_click = (value.is_click == 0)?false:true;
					})
				}
				$scope.tags = tags;

				var formDataStr = $.param({
					p: curPage,
					pageSize: 10,
					tid: id,
					uuid: uuid,
				})

				commonService.httpReq(commentUrl,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var comments = response.RESULT.comments;
						$.each(comments,function(index,value){
							value.content = $sce.trustAsHtml(commonService.html_decode(value.content));
							value.user_pic = (value.pic == "")?default_headimg_url:urlPic + value.user_pic;
							value.isGoodAble = (value.is_good==true)?true:false;
							value.isBadAble = (value.is_bad==true)?true:false;
						})
						$scope.comments = comments;
						$scope.count = response.RESULT.count;

						$(".start_loading").removeClass("loading");
					}	
				})
			})
		}
	})
	
	$scope.addTags = function($event,tagid){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			if("undefined" == typeof tagid || tagid == null){
				if("undefined" == typeof $scope.tagName || !commonService.checkTages($scope.tagName)){
					alert("印象不能为空，不能包含特殊字符，且最多10个字！");
					return false;
				}

				var formDataStr = $.param({
					tid:id,
					uuid: uuid,
					name: $scope.tagName
				})

				commonService.httpReq(putTagsUrl,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var html = "";
						html += '<li class="on">';
						html += '	<span>'+$scope.tagName+' 1'+'</span>';
						html += '</li>';

						$(".start_detail_tips_box > ul").append(html);
						//location.reload();
					}else{
						alert(response.ERRMSG);
						return false;
					}
				})
			}else{
				var input = document.getElementById("tag"+tagid);
				var span = document.getElementById("tagName"+tagid);
				var isClicking = (input.value == "false")?false:true;
				var aggre = (isClicking == false)?0:1;

				var formDataStr = $.param({
					tid:id,
					uuid: uuid,
					tagid: tagid,
					aggre: aggre
				})

				commonService.httpReq(agreeTagUrl,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var num = parseInt(span.innerText.split(" ")[1]);
						var str = span.innerText.split(" ")[0]+" ";
						span.innerText = (isClicking == false)?str+(num+1):str+(num-1);
						console.log($event.target.className)
						input.parentNode.className = (isClicking == false)? input.parentNode.className+" on" : input.parentNode.className.replace("on",'');
						input.value = (!isClicking).toString();
					}else{
						alert(response.ERRMSG);
						return false;
					}
				})
			}
		}
	}

	$scope.commentGood = function($event,id,num){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			$event.target.disabled = "true";
			var formDataStr = $.param({
				cid: id,
				uuid: uuid,
			})

			commonService.httpReq(commentGood,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					var curGood = parseInt(num);
					$event.target.innerHTML = "有道理 "+(curGood+1);
				}else{
					alert(response.ERRMSG);
				}
			})
		}
	}

	$scope.commentBad = function($event,id,num){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			$event.target.disabled = "true";
			var formDataStr = $.param({
				cid: id,
				uuid: uuid,
			})

			commonService.httpReq(commentBad,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					var curGood = parseInt(num);
					$event.target.innerHTML = "我都懂 "+(curGood+1);
				}else{
					alert(response.ERRMSG);
				}
			})
		}

	}

	$scope.assent = function(num){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			$scope.good = false;

			var formDataStr = $.param({
				tid: id,
				uuid: uuid,
			});

			commonService.httpReq(teacherGood,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					var curGood = parseInt(num);
					$(".start_good_num").children("button").html("有气质 "+(curGood+1));
					$scope.goodDisable = false;
				}else{
					alert(response.ERRMSG);
				}
			})
		}
	}

	$scope.bad = function(num){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			$scope.bad = false;

			var formDataStr = $.param({
				tid: id,
				uuid: uuid,
			});

			commonService.httpReq(teacherBad,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					var curBad = parseInt(num);
					$(".start_bad_num").children("button").html("呵呵 "+(curBad+1));
					$scope.badDisable = false;
				}else{
					alert(response.ERRMSG);
				}
			})
		}
	}

	$scope.post = function(){
		if(!userService.userIsLogin()){
			var isLoginConfirm = confirm("您还未登录，无法查看详情！点击“确定”去登录！");
			if(isLoginConfirm){
				$location.path('/user/login');
			}else{
				return;
			}
		}else{
			$scope.userIsLogin = true;

			if(!commonService.checkHtmlTags($scope.postContent)){
				alert("评论内容错误，请检查后继续评论！");
				return false;
			}

			var html = jEmoji.unifiedToHTML(jEmoji.softbankToUnified($scope.postContent));

			var formDataStr = $.param({
				eid:id,
				content: html,
				uuid: uuid,
			})

			commonService.httpReq(postUrl,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					alert("评论成功！");
					location.reload();
				}else{
					alert(response.ERRMSG);
					return false;
				}
			})
		}
	}

	$scope.input = function(){
		if($scope.postContent.length >= 30){
			$scope.canPost = true;
		}else{
			$scope.canPost = false;
		}
		$(".start_comment_tips").children("span").html(parseInt($scope.postContent.length));
	}

})